package com.situ.student.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JTextField;

import com.situ.student.dao.IStudentDao;
import com.situ.student.entity.Student;
import com.situ.student.util.JDBCUtil;

/**
 * 用jdbc来实现DAO
 * 
 * @author Gao
 *
 */
public class StudentDaoJdbcImpl implements IStudentDao {

	@Override
	public void insert(Student student) {
		int resultSet;
		Integer isid = null;
		String isname = null;
		Integer isage = null;
		String isgender = null;
		
		try {
			
			Connection connection=JDBCUtil.getConnection();
			String sql="insert into student (id,name,age,gender) values (?,?,?,?)";
			PreparedStatement preparedStatement=connection.prepareStatement(sql);
			preparedStatement.setInt(1, isid);
			preparedStatement.setString(2,isname);
			preparedStatement.setInt(3, isage);
			preparedStatement.setString(4,isgender);
			resultSet = preparedStatement.executeUpdate();
			
			
			
			
			
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	@Override
	public List<Student> list() {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		List<Student> list = new ArrayList<>();
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT id,name,age,gender FROM student";
			// 预编译sql
			preparedStatement = connection.prepareStatement(sql);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				Integer id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				Integer age = resultSet.getInt("age");
				String gender = resultSet.getString("gender");
				Student student = new Student(id, name, age, gender);
				list.add(student);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		
		return list;
	}

}
